/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DeviceGroupModel;
import org.testis.gui.mvc.view.testbed.DeviceGroupView;
import org.testis.orm.testbed.refined.DeviceGroupRefined;
import org.testis.orm.testbed.datahandlers.DeviceGroupDataHandler;
import org.testis.orm.testbed.refined.DeviceGroupFactory;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class DeviceGroupController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String DEVICEGROUP_ID_PROPERTY = "DeviceGroupId";
    public static final String NAME_PROPERTY = "Name";
    public static final String ACTIVE_PROPERTY = "IsActive";
    private TbedMutableTreeNode m_selectedNode = null;

    public DeviceGroupController() {
        super();
    }

    private void initialize(boolean editMode) {
        DeviceGroupModel model = new DeviceGroupModel();
        DeviceGroupView view = new DeviceGroupView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationNewDeviceGroup() {
        initialize(false);
        setModelProperty(ACTIVE_PROPERTY, true);
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationEditDeviceGroup(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DeviceGroupRefined) {
            m_selectedNode = node;
            DeviceGroupRefined group = getSelectedDeviceGroup();
            if (group == null) {
                System.out.println(
                        "Not found design part for DeviceGroupRefined");
                return;
            }

            initialize(true);
            setModelProperty(DEVICEGROUP_ID_PROPERTY, group.getId());
            setModelProperty(NAME_PROPERTY, group.getName());
            setModelProperty(ACTIVE_PROPERTY, group.isActive());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    private DeviceGroupRefined getSelectedDeviceGroup() {
        DeviceGroupRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DeviceGroupRefined) {
            result = (DeviceGroupRefined) obj;
        }
        return result;
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationSaveDeviceGroup(int deviceGroupId, String name) {
        boolean result = DeviceGroupDataHandler.newInstance().saveDeviceGroup(
                deviceGroupId, name);
        if (!result) {
            setModelProperty(ERROR_PROPERTY, "Unable to edit DeviceGroup record");
        }
    }

    public void operationAddDeviceGroup(String name) {
        DeviceGroupRefined groupRefined = DeviceGroupFactory.newInstance().createDeviceGroup(
                name);
        if (groupRefined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    groupRefined);
        } else {
            setModelProperty(ERROR_PROPERTY, "Unable to add DeviceGroup record");
        }
    }

    public void operationRemoveDeviceGroup(int id) {
        if (DeviceGroupDataHandler.newInstance().deleteDeviceGroup(id)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove DUT record");
        }
    }
}
